package neu.edu.vn.appmusic.utils.music;

import java.util.ArrayList;

import neu.edu.vn.appmusic.object.Song;
import neu.edu.vn.appmusic.utils.MyApplication;
import neu.edu.vn.appmusic.utils.music.MusicService.MusicBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.view.View;
import android.widget.MediaController.MediaPlayerControl;
import android.widget.Toast;

public class MyMusic implements MediaPlayerControl{
	

	private ArrayList<Song> arraySong;
	private Activity parentActivity;
	private int listViewId;
	
	//play music 
	private MusicService musicSrv;
	private Intent playIntent;
	private boolean musicBound=false;
	private MusicController controller;
	//activity and playback pause flags
	private boolean paused=false, playbackPaused=false;
	//connect to the service
	private ServiceConnection musicConnection = new ServiceConnection(){
	 
		  @Override
		  public void onServiceConnected(ComponentName name, IBinder service) {
		    MusicBinder binder = (MusicBinder)service;
		    //get service
		    musicSrv = binder.getService();
		    //pass list
		    musicSrv.setList(arraySong);
		    musicBound = true;
		  }

			@Override
			public void onServiceDisconnected(ComponentName name) {
				// TODO Auto-generated method stub
				
			}
	};
	
	public MyMusic() {
		super();
	}

	public MyMusic(Activity parentActivity, int listViewId, ArrayList<Song> listSong) {
		super();
		this.parentActivity = parentActivity;
		this.listViewId = listViewId;
		this.arraySong = listSong;
	}
	
	public void initMusic(){
		setController();
		if (playIntent == null) {
//			playIntent = new Intent(parentActivity, MusicService.class);
//			parentActivity.bindService(playIntent, musicConnection, Context.BIND_AUTO_CREATE);
//			parentActivity.startService(playIntent);
			playIntent = new Intent(MyApplication.getInstance().getApplicationContext(),MusicService.class);
			MyApplication.getInstance().bindService(playIntent, musicConnection, Context.BIND_AUTO_CREATE);
			MyApplication.getInstance().startService(playIntent);
		}
	}
	
	public void initMusic(Activity parentActivity, int listViewId, ArrayList<Song> listSong){
		if(parentActivity.equals(this.parentActivity))
			return;
		this.parentActivity = parentActivity;
		this.listViewId = listViewId;
		this.arraySong = listSong;
		setController();
		if (playIntent == null) {
//			playIntent = new Intent(parentActivity, MusicService.class);
//			parentActivity.bindService(playIntent, musicConnection, Context.BIND_AUTO_CREATE);
//			parentActivity.startService(playIntent);
			playIntent = new Intent(MyApplication.getInstance().getApplicationContext(),MusicService.class);
			MyApplication.getInstance().bindService(playIntent, musicConnection, Context.BIND_AUTO_CREATE);
			MyApplication.getInstance().startService(playIntent);
		}
	}
	
	//set the controller up
	private void setController(){
		controller = new MusicController(parentActivity);
		//set previous and next button listeners
		controller.setPrevNextListeners(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				playNext();
			}
		}, new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				playPrev();
			}
		});
		//set and show
		controller.setMediaPlayer(this);
		controller.setAnchorView(parentActivity.findViewById(listViewId));
		controller.setEnabled(true);
	}
	
	private void playNext(){
		musicSrv.playNext();
		if(playbackPaused){ 
			setController();
			playbackPaused=false;
		}
		controller.show(0);
	}

	private void playPrev(){
		musicSrv.playPrev();
		if(playbackPaused){
			setController();
			playbackPaused=false;
		}
		controller.show(0);
	}
	
	public void songPicked(int position){
		musicSrv.setSong(position);
		musicSrv.playSong();
		if(playbackPaused){
			setController();
			playbackPaused=false;
		}
		controller.show(0);
	}

	@Override
	public int getDuration() {
		if(musicSrv!=null && musicBound && musicSrv.isPng())
			return musicSrv.getDur();
		else return 0;
	}

	@Override
	public int getCurrentPosition() {
		if(musicSrv!=null && musicBound && musicSrv.isPng())
			return musicSrv.getPosn();
		else return 0;
	}
	
	@Override
	public boolean isPlaying() {
		if(musicSrv!=null && musicBound)
			return musicSrv.isPng();
		return false;
	}

	@Override
	public void pause() {
		playbackPaused=true;
		musicSrv.pausePlayer();
	}

	@Override
	public void seekTo(int pos) {
		musicSrv.seek(pos);
	}

	@Override
	public void start() {
		musicSrv.go();
	}


	@Override
	public int getBufferPercentage() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public boolean canPause() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean canSeekBackward() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean canSeekForward() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public int getAudioSessionId() {
		// TODO Auto-generated method stub
		return 0;
	}
	
	
	//Su kien cua Activity
	
	public void onPause(){
		paused=true;
	}
	
	public void onResume(){
		if(paused){
			setController();
			paused=false;
		}
	}
	
	public void onStop() {
		controller.hide();
	}
	
	public void onDestroy() {
//		Toast.makeText(MyApplication.getAppContext(), "Stop Service", Toast.LENGTH_SHORT).show();
		MyApplication.getInstance().stopService(playIntent);
		parentActivity.stopService(playIntent);
		musicSrv=null;
	}

}
